//
//  BUScrollAnimationView.m
//  BUBU3D
//
//  Created by lichentao on 14-7-13.
//  Copyright (c) 2014年 chentaoApp. All rights reserved.
//  动画view

#import "BUScrollAnimationView.h"
#import "Constants.h"
#import "BPCommon.h"
#import "UIImageView+AFNetworking.h"
#import "UIView+Addition.h"
#define HEIGHT_SCROLL 271.f

@implementation BUScrollAnimationView
@synthesize dmImgArray;

- (void)viewDidLoad{
    [super viewDidLoad];

    dmImgArray = [[NSMutableArray alloc] init];
    headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, HEIGHT_SCROLL/2)];
    headView.backgroundColor = [UIColor blackColor];
    
    pageCtr = [[UIPageControl alloc]initWithFrame:CGRectMake(100, HEIGHT_SCROLL/2, 120, 20)];
    pageCtr.pageIndicatorTintColor = [UIColor whiteColor];
    
    scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, HEIGHT_SCROLL/2)];
    scrollview.pagingEnabled = YES;
    scrollview.delegate = self;
    scrollview.showsVerticalScrollIndicator = NO;
    scrollview.showsHorizontalScrollIndicator = NO;
    
    [headView addSubview:scrollview];

    [self.view addSubview:headView];
    [self startTimer];
}


- (void)startAnimationWihtImgArray:(NSMutableArray *)inputArray BUAnimationBlock:(BUAnimationBlock)animationBlock{
    
    self.dmImgArray = inputArray;
    self.animationBlock = animationBlock;
    scrollview.contentSize = CGSizeMake(ScreenSize.width * [dmImgArray count] ,HEIGHT_SCROLL/2);
    
    imgViewsArray = [NSMutableArray array];
    
    for (int i=0; i<[dmImgArray count]; i++) {
        DmModel *entity = [dmImgArray objectAtIndex:i];
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenSize.width*i, 0, ScreenSize.width, HEIGHT_SCROLL/2)];
        image.userInteractionEnabled = YES;
        image.tag = i+100;
        [image setImageWithURL1:[NSURL URLWithString:entity.dmUrl]];
        
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dmAction:)];
        [image addGestureRecognizer:gestureRecognizer];
        
        [imgViewsArray addObject:image];
        
        UIView *bgView = [BPCommon addBackView:CGRectMake(0, HEIGHT_SCROLL/2-40, 320, 40) setBackColor:[UIColor blackColor]];
        bgView.alpha = 0.5f;
        
        titleLabel = [BPCommon addLabel:CGRectMake(10,HEIGHT_SCROLL/2-40, 300, 40) setTitle:entity.dmName setBackColor:[UIColor whiteColor] setFont:[UIFont boldSystemFontOfSize:20]];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [image addSubview:bgView];
        [image addSubview:titleLabel];
        
        [scrollview addSubview:image];
    }
    pageCtr.numberOfPages = [dmImgArray count];
    [headView addSubview:pageCtr];
    if ([self.dmImgArray count] == 0) {
        [scrollview removeAllSubviews];
    }

}

// 开始timer
- (void)startTimer{
    _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(autoChangePage) userInfo:nil repeats:YES];
}

//// 开始动画
#pragma mark ------------   private
-(void)autoChangePage
{
    if (pageCtr.currentPage < (pageCtr.numberOfPages-1)) {
        pageCtr.currentPage += 1;
        [scrollview setContentOffset:CGPointMake(pageCtr.currentPage*ScreenSize.width, 0) animated:YES];
    } else {
        pageCtr.currentPage =0;
        [scrollview setContentOffset:CGPointMake(pageCtr.currentPage*ScreenSize.width, 0)];
    }
}

#pragma mark ----------------------- UIScrollView delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == scrollview) {
        
    }
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    int size  = self.view.frame.size.width;
    int page = scrollView.contentOffset.x/size;
    pageCtr.currentPage = page;

}

- (void)dmAction:(id)sender{
    UIGestureRecognizer *imgGesture = (UITapGestureRecognizer *)sender;
    
    DmModel *entity = [dmImgArray objectAtIndex:imgGesture.view.tag-100];
    _animationBlock(entity,(UIImageView *)imgGesture.view,imgViewsArray);
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
